package com.shopping.dao;

import com.shopping.pojo.OfflineMessage;
import com.shopping.pojo.OfflineSys;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Vector;

@Component
@Mapper
public interface OfflineDao {

    String TABLE_NAME_SYS = "offline_sys";
    String INSERT_FIELDS_SYS = "sender_id, receiver_id";
    String SELECT_FIELDS_SYS = "id, sender_id senderId, receiver_id receiverId";

    String TABLE_NAME_MESSAGE = "offline_message";
    String INSERT_FIELDS_MESSAGE = "sys_id, message";
    String SELECT_FIELDS_MESSAGE = "id, sys_id sysId, message";

    @Insert({"insert into ", TABLE_NAME_SYS, "(", INSERT_FIELDS_SYS, ") values(#{senderId}, #{receiverId})"})
    int insertIntoSys(OfflineSys offlineSys);

    @Insert({"insert into ", TABLE_NAME_MESSAGE, "(", INSERT_FIELDS_MESSAGE, ") values(#{sysId}, #{message})"})
    int insertIntoMessage(OfflineMessage offlineMessage);

    @Select({"select ", SELECT_FIELDS_SYS, " from ", TABLE_NAME_SYS, " where receiver_id=#{receiverId}"})
    List<OfflineSys> selectFromSys(@Param("receiverId") int receiverId);

    @Select({"select ", SELECT_FIELDS_MESSAGE, " from ", TABLE_NAME_MESSAGE, " where sys_id=#{sysId}"})
    Vector<OfflineMessage> selectFromMessage(@Param("sysId") int sysId);

    @Delete({"delete from ", TABLE_NAME_SYS, " where sender_id=#{senderId} and receiver_id=#{receiverId}"})
    int deleteFromSys(@Param("senderId") int senderId, @Param("receiverId") int receiverId);

    @Delete({"delete from ", TABLE_NAME_MESSAGE, " where sys_id=#{sysId}"})
    int deleteFromMessage(@Param("sysId") int sysId);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();

}
